*=$0801
; 2018 SYS 2062
 !word main-2, 10 
 !byte $9e
 !text " 2062"
 !byte $00,$00,$00


main
    sei
    lda #$80  ; noise waveform, gate bit off
    sta $d412 ; voice 3 control register
    lda #$ff  ; maximum frequency value
    sta $d40f ; voice 3 frequency high byte

    jsr setupgfx
    
startagain
    jsr waitnextframe
    jsr moveplayer
    jsr shiftscreen

    lda $d01f
    beq startagain
  
    ldx #100
flash
    inc $d020
    inc $d021
    jsr waitnextframe
    dex
    bne flash
  
    jsr setupgfx
    jmp startagain
    

moveplayer
    ; https://www.c64-wiki.com/wiki/Keyboard
      lda #%11111111  ; CIA#1 port A = outputs 
      sta $dc02             

      lda #%00000000  ; CIA#1 port B = inputs
      sta $dc03

         ldx #%11111101  ; testing column 1 (COL1) of the matrix
         lda #%00100000  ; masking row 5 (ROW5) 
         jsr testkey
         bne nos
         inc $d001
nos
         lda #%00000100  ; masking row 5 (ROW5) 
         jsr testkey2
         bne noa
         dec $d000
         lda $d000
         cmp #$ff
         bne noa
         lda andbits
         eor $d010
         sta $d010
noa
         lda #%00000010  
         jsr testkey2
         bne now
         dec $d001
now
         ldx #%11111011  ; testing column 1 (COL1) of the matrix
         lda #%00000100 
         jsr testkey
         bne nod
         inc $d000
         bne nod
         lda andbits
         eor $d010
         sta $d010
nod
rts

;x = row, a = test mask
testkey
    stx $dc00
testkey2 ; if dc00 isn't changing
    and $dc01   
    rts
    
shiftscreen ;Shift the screen
    inc scrollx  
    lda scrollx
    and #7
    ora #0 ; #16
    sta $d016
    and #7
    bne scrend

    ;We are back to position 0, so do the scrolling
    ldx #<(1024-40)
    stx scrlmove+4
    dex
    stx scrlmove+1 
    ldx #>(1024-40)
    stx scrlmove+5
    stx scrlmove+2

    ldx #25
scrlline
    clc
    lda scrlmove+4
    adc #40
    sta scrlmove+4
    sta scrladd+1
    lda scrlmove+5
    adc #0
    sta scrlmove+5
    sta scrladd+2
    
    ;scrlmove+=40
    clc
    lda scrlmove+1
    adc #40
    sta scrlmove+1
    lda scrlmove+2
    adc #0
    sta scrlmove+2
    
    ldy #39
scrlmove
    lda 1024,y
    sta 1025,y
    dey
    bne scrlmove

   cpx #1
   beq scrsolid
   cpx #25
   beq scrsolid
    ldy #32
    lda $d41b 
    cmp #8
    bcs scrladd
scrsolid
    ldy #160
scrladd
    sty $0400
    dex
    bne scrlline
scrend
     rts

setupgfx 
      lda #0
      sta $d020
      sta $d021
      lda #6
      sta $d022
      lda #14
      sta $d023
      
      ldx #0
clearscn      ;sclear screen
      lda #32
      sta $0400,x
      sta $0500,x
      sta $0600,x
      sta $0700-24,x
      lda #15
      sta $d800,x
      sta $d900,x
      sta $da00,x
      sta $db00-24,x
      inx
      bne clearscn
      lda #200
      sta $d000
      lda #100
      sta $d001
        lda #2
        sta $d027
        
        ldx #0
sprp    lda #$ff  ;Sprite pointers
        sta $07f8,x
        inx
        cpx #8
        bne sprp
        
        
          ldx #0    
sprd    lda spribits,x  ;Set up sprite data
        sta $3fc0,x
        inx
        cpx #64
        bne sprd

        lda #$1
        sta $d015
    
           lda #$18
    sta $d018
    
    ldx #0  ;Make character set
cloop
    lda #0
    sta $2000,x
    sta $2100,x
    sta $2200,x
    sta $2300,x
    sta $2400,x
    sta $2500,x
    sta $2600,x
    sta $2700,x
    inx
    bne cloop

    
 
    lda $d01f
        

    ldx #0
    lda #$ff
 floop
    sta $2000+160*8,x
    inx
    cpx #8
    bne floop
    lda #0
    sta $2000+32*8+3
    sta $2000+0*8+3
        rts
        
waitnextframe
waitline 
    lda #$ff
    cmp $d012
    bne waitline
wait0 ; avoid rushing
    lda $d012
    bne wait0
rts

scrollx !byte 0
  

spribits
  !byte 0,127,0,1,255,192,3,255,224,3,231,224
  !byte 7,217,240,7,223,240,7,217,240,3,231,224
  !byte 3,255,224,3,255,224,2,255,160,1,127,64
  !byte 1,62,64,0,156,128,0,156,128,0,73,0,0,73,0
  !byte  0,62,0,0,62,0,0,62,0,0,28,0 

andbits
 !byte 1,1,2,2,4,4,8,8,16,16,32,32,64,64,128,128